package com.all.config.caffeine;

import com.all.config.LocalCacheFactory;
import com.all.config.LocalCacheService;
import com.github.benmanes.caffeine.cache.Cache;

/**
 * @Author LY
 * @create 2024/6/3 15:02
 * @Description 基于Caffeine实现的本地缓存
 */
public class CaffeineLocalCacheService<K, V> implements LocalCacheService<K, V> {

    /**
     * 本地缓存，基于Guava实现
     */
    private final Cache<Object, Object> cache = LocalCacheFactory.getCaffeineLocalCacheSync();

    @Override
    public void put(K key, V value) {
        cache.put(key, value);
    }

    @Override
    public V getIfPresent(Object key) {
        return (V) cache.getIfPresent(key);
    }

    @Override
    public void delete(Object key) {
        cache.invalidate(key);
    }

    public static void main(String[] args) {
        LocalCacheService cacheService = new CaffeineLocalCacheService();
        String key = "caf";
        String value = "001";
        cacheService.put(key, value);
        System.out.println(cacheService.getIfPresent(key));
    }
}
